package com.allwees.bs.task.order.notify;

import com.allwees.bs.core.model.order.config.ScheduleTimeProperties;
import com.allwees.bs.module.oms.dao.OrderDao;
import com.allwees.bs.module.oms.dao.OrderSkuSnapDao;
import com.allwees.bs.module.oms.entity.Order;
import com.allwees.bs.module.oms.entity.OrderSkuSnap;
import com.allwees.bs.module.ums.dao.DeviceDao;
import com.allwees.bs.module.ums.entity.Device;
import com.allwees.core.common.util.DateUtil;
import com.allwees.sender.AppSender;
import com.allwees.sender.MessageSender;
import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;


@Slf4j
@Service

public class OrderUnPaidNotifyTask {
    private final static String LINK = "AccountOrderDetailPage";
    private final static String NOTIFY_TITLE = "Order No. #{orderNo} unpaid";
    private final static String NOTIFY_CONTENT = "Please pay the unpaid order no. #{orderNo}. We wish you happy shopping!";

    @Autowired
    private ScheduleTimeProperties scheduleTimeProperties;

    @Autowired
    private OrderDao orderDao;

    @Autowired
    private OrderSkuSnapDao orderSkuSnapDao;

    @Autowired
    private DeviceDao deviceDao;

    @Autowired
    private MessageSender messageSender;

    @XxlJob(value="orderUnPaidNotifyTask")
    public ReturnT<String> run(String s) {
        doTask();
        return ReturnT.SUCCESS;
    }

    private void doTask() {
        if(scheduleTimeProperties.getOrderUnPaidNotifySecond() == null) {
            return;
        }

        Date now = new Date();
        Date paymentExpiredFrom = DateUtil.addSeconds(now, scheduleTimeProperties.getOrderUnPaidNotifySecond());
        List<Order> unPaidOrders = orderDao.findUnPaidUnExpireOrderList(paymentExpiredFrom,now);
        if (CollectionUtils.isEmpty(unPaidOrders)) {
            return;
        }
        Set<String> userUuids = unPaidOrders.stream().map(Order::getUserUuid).collect(Collectors.toSet());

        List<Device> devices = deviceDao.findByUserUuids(userUuids);
        Map<String, Device> deviceMap = devices.stream().collect(Collectors.toMap(Device::getUserUuid, o -> o));

        for (Order order : unPaidOrders) {
            Device device = deviceMap.get(order.getUserUuid());
            String jpushId = device != null ? device.getJpushId() : null;
            send(jpushId, order.getUserUuid(), order.getUuid(), order.getNo(), getMainPhoto(order.getUuid()));
        }
    }

    private void send(String jpushId, String userUuid, String orderUuid,String orderNo,String mainPhoto) {
        String title = NOTIFY_TITLE.replace("#{orderNo}", orderNo);
        String content = NOTIFY_CONTENT.replace("#{orderNo}", orderNo);

        if (StringUtils.isNotBlank(jpushId)) {
            AppSender.push(jpushId, title, content);
        }
        Map<String, String> params = new HashMap<>();
        params.put("orderUuid", orderUuid);
        messageSender.send(userUuid, title, content, LINK, mainPhoto, params);
    }

    private String getMainPhoto(String orderUuid) {
        List<OrderSkuSnap> skuSnaps = orderSkuSnapDao.findByOrderUuid(orderUuid);
        if(CollectionUtils.isEmpty(skuSnaps)){
            return "";
        }
        return skuSnaps.get(0).getProductPhoto();
    }
}
